//
//  WSChannelMoreRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/1.
//

#import "WSChannelMoreRequest.h"

@implementation WSChannelMoreRequest

+ (void)loadcharonTV_RankWithSort_type:(NSInteger)sort_type page:(NSInteger)page success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_CHANNEL_MORE;
    NSDictionary * charonTV_parameters = @{@"sort_type":@(sort_type),
                                           @"page":@(page)
    };
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSMutableArray * charonTV_array = [WSVideo mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(charonTV_array);
    } failure:failed];
}

#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.page = 1;
    self.haveMore = YES;
    [WSChannelMoreRequest loadcharonTV_RankWithSort_type:self.charonTV_sort_type page:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [WSChannelMoreRequest loadcharonTV_RankWithSort_type:self.charonTV_sort_type page:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(charonTV_ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = array.count == 10;
    self.page++;
    self.loading = NO;
    success(array);
}

@end
